$(function () {
    $("#myTabs").tabs({
        fit: true
    })

    $("#myTree").tree({
        url: "/menu/selectByParentSn.do?sn=system",
        onClick: function (node) {
            var roots = $("#myTree").tree("getRoots");
         //   console.log(roots)

        //    console.log(node)
            if (node.url) {
                if ($("#myTabs").tabs("exists", node.text)) {
                    $("#myTabs").tabs("select", node.text);
                } else {
                    reloadTree(node);
                }
            }
        },
        onLoadSuccess : function (node, data) {
           $(data).each(function (index,value) {
            //   console.log(value)
               if(index != 0){
                  value.state = 'closed';
               }
           })
        }
    })

    $.extend($.fn.validatebox.defaults.rules, {
        equals: {
            validator: function (value, param) {
                return value == $(param[0]).val();
            },
            message: '输入的密码不一致.'
        }
    });

    $("#accordionTree").accordion({
        border: false,
        onSelect: function (title, index) {
            var row = $("#accordionTree").accordion("getPanel", index);
            var data = $(row).data("sn");
            if (data) {
                var url = "/menu/selectByParentSn.do?sn=" + data;
                $(row).find("ul").tree({
                    url: url,
                    onClick: function (node) {
                        if ($("#myTabs").tabs("exists", node.text)) {
                            $("#myTabs").tabs("select", node.text);
                        } else {
                            reloadTree(node);
                        }
                    }
                })
            }

        }
    })


    function reloadTree(node) {
        $("#myTabs").tabs("add", {
            plain: true,
            title: node.text,
            closable: true,
            selected: true,
            content: "<iframe src='" + node.url + "' width='100%' height='99.9999%' frameborder='0' border='0' ></iframe>"
        })
    }
})